package com.xinsoft.controller.sale;

import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.ScmSaleReturnDTO;
import com.xinsoft.entity.dto.ScmSaleReturnParam;
import com.xinsoft.entity.vo.ScmSaleReturnListVo;
import com.xinsoft.entity.vo.ScmSaleReturnVo;
import com.xinsoft.service.ScmSaleReturnService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 云供应链/销售管理/销售退货
 */
@RestController
@RequestMapping("/scm/saleReturn")
public class ScmSaleReturnController {
    @Autowired
    private ScmSaleReturnService saleReturnService;

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody ScmSaleReturnParam param) {
        return Response.succeed(saleReturnService.findPageListByParam(param));
    }

    /**
     * 暂存
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody ScmSaleReturnDTO scmSaleReturnDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(scmSaleReturnDTO);
        saleReturnService.tempSave(scmSaleReturnDTO);
        return Response.succeed();
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<ScmSaleReturnVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        return Response.succeed(saleReturnService.detail(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        List<Integer> newIds = ids.stream().distinct().collect(Collectors.toList());
        saleReturnService.del(newIds);
        return Response.succeed();
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") ScmSaleReturnParam param, HttpServletResponse response) throws Exception {
        List<ScmSaleReturnListVo> list = saleReturnService.findListByParam(param);
        ExcelUtils.exportByEasypoi(list, ScmSaleReturnListVo.class, "销售退货", "销售退货", response, "销售退货");
        return Response.succeed();
    }

    /**
     * 审核(保存或更新 并审核)
     */
    @PostMapping("/addOrUpdateAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdateAudit(@RequestBody ScmSaleReturnDTO scmSaleReturnDTO){
        // 参数校验
        ValidatorUtils.validateEntity(scmSaleReturnDTO);
        saleReturnService.addOrUpdateAudit(scmSaleReturnDTO);
        return Response.succeed();
    }

    /**
     * 审核
     */
    @GetMapping("/audit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> audit(@ApiParam(value = "销售退货单id",required = true) @RequestParam("id") Integer id){
        saleReturnService.audit(id);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @GetMapping("/reverseAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> reverseAudit(@ApiParam(value = "销售退货单id",required = true) @RequestParam("id") Integer id){
        saleReturnService.reverseAudit(id);
        return Response.succeed();
    }
}
